// pages/search/search.js
import ajax from "../../utils/ajax";
import api from "../../utils/api";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchWord: "",
    histroyList: [{id:0,name:"零食"},{id:1,name:"男装"}],
    recommendList: [{id:0,name:"母婴"},{id:1,name:"家具"}],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '搜索',
    })

    this.getHistroyList();
    this.getRecommendList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  // 获取历史列表
  getHistroyList() {
    let that = this;
    // ajax.ajax(api.HistroyList, {})
    //   .then((val) => {
    //     console.log(val)
    //     if (val.Tag == 1 && val.Result) {
    //       that.setData({
    //         histroyList: val.Result.list
    //       })
    //     } else {
    //       wx.showToast({title: "加载失败，请重试", icon: 'none'})
    //     }
    //   })
    //   .catch((val) => {
    //     wx.showToast({title: "加载失败，请重试", icon: 'none'})
    //   })
  },

  // 获取推荐列表
  getRecommendList() {
    let that = this;
    // ajax.ajax(api.RecommendList, {})
    //   .then((val) => {
    //     console.log(val)
    //     if (val.Tag == 1 && val.Result) {
    //       that.setData({
    //         recommendList: val.Result.list
    //       })
    //     } else {
    //       wx.showToast({title: "加载失败，请重试", icon: 'none'})
    //     }
    //   })
    //   .catch((val) => {
    //     wx.showToast({title: "加载失败，请重试", icon: 'none'})
    //   })
  },

  // 清除历史
  clearHistroy() {
    let that = this;
    // ajax.ajax(api.RecommendList, {})
    //   .then((val) => {
    //     console.log(val)
    //     if (val.Tag == 1) {
    //       that.setData({
    //         histroyList: []
    //       })
    //     } else {
    //       wx.showToast({title: "加载失败，请重试", icon: 'none'})
    //     }
    //   })
    //   .catch((val) => {
    //     wx.showToast({title: "加载失败，请重试", icon: 'none'})
    //   })

    // 调试
    that.setData({
      histroyList: []
    })
  },

  // 点击搜索
  handleClickSearch() {
    let word = this.data.searchWord;
    if (!word) {return;}
    this.confirmSearch(word);
  },

  // 带参数跳转列表页
  confirmSearch(word) {
    wx.navigateTo({
      url: '/pages/productList/productList?search='+word,
    })
  },

  // 点击选择历史项
  chooseHistroyItem(e) {
    let index = e.currentTarget.dataset.index;
    let histroy = this.data.histroyList;
    this.confirmSearch(histroy[index].name);
  },

  // 点击选择推荐项
  chooseRecommendItem(e) {
    let index = e.currentTarget.dataset.index;
    let recommend = this.data.recommendList;
    this.confirmSearch(recommend[index].name);
  },
})